<template>
  <div :class="className" :style="{ height: height, width: width}" />
</template>

<script>
var echarts = require('echarts/lib/echarts')
// 引入 ECharts 主模块
require('echarts/lib/chart/line')
require('echarts/lib/chart/bar')
require('echarts/lib/chart/custom')
// 引入提示框和标题组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/legend')
import { debounce } from '@/utils'
import { themeColor } from '@/assets/echarts/theme'

export default {
  name: 'AutoAnalyzeWarningBarYCategory',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    data: {
      type: Object,
      default: () => ({})
    },
    yAxle: {
      type: Array,
      default: () => ([])
    }
  },
  data() {
    return {
      chart: null
    }
  },
  computed: {},
  watch: {
    data: {
      handler() {
        this.initChart()
      }
    }
  },
  mounted() {
    // this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const arr = []
      for (const item in this.data.binRangeDictionary) {
        arr.push(this.data.binRangeDictionary[item].join('-'))
      }
      const processRatioDatax = []
      this.data.chartSetting.series[0].data
      const dataMax = parseFloat(this.data.chartSetting.yAxis[1].dataMax)
      const dataMin = parseFloat(this.data.chartSetting.yAxis[1].dataMin)
      var i = 0
      // 第一次见这种for循环,大受震撼
      for (i = 0; i < this.data.chartSetting.yAxis[1].data.length; i++) {
        const iscontinue = parseFloat(this.data.chartSetting.yAxis[1].data[i]) >= parseFloat(dataMin) && parseFloat(this.data.chartSetting.yAxis[1].data[i]) <= parseFloat(dataMax)
        // eslint-disable-next-line no-constant-condition
        if (iscontinue) {
          processRatioDatax.push(this.data.chartSetting.series[1].data[i])
        }
      }
      this.chart = echarts.init(this.$el, 'themeColor')
      this.chart.setOption({
        color: themeColor,
        tooltip: {
          trigger: 'axis',
          extraCssText: 'transform:rotate(-90deg)',
          position: [0, 150]
        },
        grid: {
          left: 50,
          containLabel: false,
          bottom: 10,
          right: 10
        },
        xAxis: [
          {
            name: 'Count',
            type: 'category',
            show: false,
            // data: (this.data.chartSetting.series[0].data),
            data: arr.reverse()
            // axisPointer: {
            //   type: 'shadow'
            // }
          },
          {
            name: 'Ratio',
            type: 'category',
            show: false
          }
        ],
        yAxis: [
          {
            name: 'Count',
            type: 'value',
            position: 'right',
            data: [],
            show: false,
            axisLine: {
              show: false
            },
            axisLabel: {
              show: false
            }
          },
          {
            name: 'Ratio',
            type: 'value',
            position: 'left',
            show: false,
            axisLine: {
              show: false
            },
            axisLabel: {
              show: false
            }
          }
        ],
        series: [
          {
            name: 'Count',
            type: 'bar',
            data: (this.data.chartSetting.series[0].data).reverse()
          },
          {
            type: 'line',
            smooth: true,
            yAxisIndex: 1,
            xAxisIndex: 1,
            data: processRatioDatax.reverse()
          }
        ]
      })
    }
  }
}
</script>
